
using UnityEngine;
public class Bee : CObject{
	
    public static Image ImgBalloon;
    public static Image[] ImgBee = new Image[6];
    public static Image ImgBee1;

	public static int WBalloon, HBalloon;
	public int status; // 0 song 1 chet
	public static byte NORMAL = 0;
	public static byte SO_FAST = 1;
    public static byte MOVE_UP = 3;
    public static byte MOVE_DOWN = 4;
	public bool cmtoBee;
    public int vBee = 0;
    public int vBeedown = 0;
	int idframe = 0;
	int indexFrame=0;//index of frame char
	int delayFrame=0;//thoi gian thuc hien 1 frame
	public bool cmtoChar;
	public byte cdir;
	Map map = new Map();
	public Bee(){
		DataGame.LoadChar ();//load info file char
		reset ();
        loadImage();
	}
	
	private void loadImage(){
        if(ImgBalloon == null){
            ImgBalloon = CCanvas.loadImage(Main.res + "/balloon");
        }
        if (ImgBee1 == null)
        {
            ImgBee1 = CCanvas.loadImage(Main.res+"/charbee");
        }
        for (int i = 0; i < ImgBee.Length; i++)
        {
            if(ImgBee[i] == null)
                ImgBee[i] = CCanvas.loadImage(Main.res+"/char/"+i);     
        }
	}
    int tempX, tempY;
    public int timedelay;
    int temp;
    public void updatekey() 
    {
        if (CCanvas.keyPressedz[2])//key up
        {
            status = MOVE_UP;
            vBee = -15;
			y+=vBee;
        }
    }
	//khoi tao vi tri ban dau cho bee
	private void reset()
	{
		x =	Screen.width /3;
		y = Screen.height / 3;
		status = MOVE_DOWN;
		idframe = 0;
		vBee = 0;
	}

	//chay qua cac index cua frame
	void RunIndexChar()
	{
		delayFrame++;
		if(delayFrame>3)
		{
			if(indexFrame<DataGame.allFrameChars.Length-1)
			{
				indexFrame++;
			}
			else
			{
				indexFrame=0;
			}
			delayFrame=0;
		}
	}
	int t = 0;//su dung de delay
    public void moveBee()
    {// di chuyen cua BEe

		vx = 1;
        updatePoint();
        updatekey();
        if (status == MOVE_UP)
        {
            vBee +=1;
			vy =vBee ;
            if (vBee >= 0)
            {
                status = MOVE_DOWN;
                vBeedown = 0;
            }
        }

        if (status == MOVE_DOWN)
        {
			t++;
			if(t>2)
			{
				vBeedown += 1;
				t=0;
			}
            
			vy = vBeedown;
        }


        CCanvas.clearKeyPressed();  
    }
	
	public void updatePoint(){
		if(CCanvas.isPointerDown){
			CCanvas.keyPressedz[2] = true;
		}
		CCanvas.clearPoint();
	}
    int dem = 0;
   
	public void update(){
       
		RunIndexChar ();
        dem++;			
		moveBee();
		if (cmtoChar) 
		{
			GameScr.cmtoX = x - GameScr.gW2 + GameScr.gW6 * cdir;
			GameScr.cmtoY = y - GameScr.gH23;
		}
		//dead
		if (y > Screen.height || x < 0 || y < 0)
		{
			reset();
		}
		move (vx,vy);
	}
	
	public  void paint(mGraphics g){


       //g.drawImageScale(ImgBee[idframe], x, y, ImgBee[idframe].getWidth(), ImgBee[idframe].getHeight(), 0);

		int nFrameImage=DataGame.allFrameChars[indexFrame].frameImages[DOWN].size();
		for(int j=0;j<nFrameImage;j++)
		{

			DataFrameImage imageFrame=(DataFrameImage) DataGame.allFrameChars[indexFrame].frameImages[DOWN].elementAt(j);

			DataSmallImageFrame smallImage=(DataSmallImageFrame) DataGame.smallImageFrame.elementAt(imageFrame.idSmallImage);

			Image img=DataGame.LoadImage(smallImage.bigImage+20000);
			g.drawImageScaleRegion(img,smallImage.x,smallImage.y,smallImage.width,smallImage.height,imageFrame.x + x,imageFrame.y + y, 0);
		} 

	}
}
